package com.piedra.reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxyDemo {
	public static void main(String[] args) {
		Target t = new Target();
		FilterHandler fh = new FilterHandler(t);
		Realable proxy = (Realable) Proxy.newProxyInstance(t.getClass().getClassLoader(),t.getClass().getInterfaces(), fh);
		proxy.print();
	}
	
}

interface Realable{
	public void print();
}

class Target implements Realable{
	public void print(){
		System.out.println("target method invoke.");
	}
}

class FilterHandler implements InvocationHandler {
	private Object target;
	
	public FilterHandler(Object target){
		this.target = target;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("before  " + method.getName() + " invoke .");
		method.invoke(target, args);
		System.out.println("after  " + method.getName() + " invoke .");
		return null;
	}
	
}